import { wxToLogin,isHttp } from "@/utils/tools";

// const baseUrl = "/gzh-api";
const baseUrl = "https:///prosystem.panasonic.cn/gzh-api";
// const baseUrl = "https://sxgzh.autoeco.com.cn/gzh-api";

function request({ url, params, method = "GET", header = {} }) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: isHttp(url) ? url : (baseUrl + url),
      data: params,
      method,
      header: {
        ...header,
        "third-session": uni.getStorageSync("token"),
      },
      withCredentials: true,
      success: (res) => {
        if (res.statusCode == 200) {
          if (res.data.code == 200) {
            resolve(res.data);
          } else {
            if (res.data.code == 60002 || res.data.code==60001) {
              uni.removeStorageSync("token");
              wxToLogin();
            }
            reject(res.data);
          }
        } else {
          reject(res);
        }
      },
      fail: (err) => {
        reject(err);
      },
    });
  });
}

export default request;
